using System;
using System.Data;
using PugLib;

namespace ParallelTest
{
    public partial class Benchmarks
    {
        const int SelectCount = 100000;
        const int SelectRowCount = SelectCount / 10;

        public static TestResult DataRowSelectNonIndexed()
        {
            const string name = "DataRowSelectNonIndexed";
            Console.WriteLine(name);
            TestResult trd = new TestResult(name);

            DataSet ds = new DataSet(name);
            ds.Tables.Add(GetTestDataTable(name, false, false, SelectRowCount));
            stopwatch.Reset();
            stopwatch.Start();

            for (int i = 0; i < SelectCount; i++)
            {
                ds.Tables[0].Select(string.Format("{0} = {1} and {2} = {3}", "a", i, "b", i));
            }

            stopwatch.Stop();
            ds.Dispose();
            trd.Duration = stopwatch.Elapsed.TotalSeconds;
            return trd;
        }

        public static TestResult DataRowSelectIndexedOnLong()
        {
            const string name = "DataRowSelectIndexedOnLong";
            Console.WriteLine(name);
            TestResult trd = new TestResult(name);

            DataSet ds = new DataSet(name);
            ds.Tables.Add(GetTestDataTable(name, true, false, SelectRowCount));
            stopwatch.Reset();
            stopwatch.Start();

            for (int i = 0; i < SelectCount; i++)
            {
                ds.Tables[0].Select(string.Format("{0} = {1} and {2} = {3}", "a", i, "b", i));
            }

            stopwatch.Stop();
            ds.Dispose();
            trd.Duration = stopwatch.Elapsed.TotalSeconds;
            return trd;
        }

        public static TestResult DataRowSelectIndexedOnFirstThree()
        {
            const string name = "DataRowSelectIndexedOnFirstThree";
            Console.WriteLine(name);
            TestResult trd = new TestResult(name);

            DataSet ds = new DataSet(name);
            ds.Tables.Add(GetTestDataTable(name, false, true, SelectRowCount));
            stopwatch.Reset();
            stopwatch.Start();

            for (int i = 0; i < SelectCount; i++)
            {
                ds.Tables[0].Select(string.Format("{0} = {1} and {2} = {3}", "a", i, "b", i));
            }

            stopwatch.Stop();
            ds.Dispose();
            trd.Duration = stopwatch.Elapsed.TotalSeconds;
            return trd;
        }

        public static TestResult DataRowSelectIndexedOnAll()
        {
            const string name = "DataRowSelectIndexedOnAll";
            Console.WriteLine(name);
            TestResult trd = new TestResult(name);

            DataSet ds = new DataSet(name);
            ds.Tables.Add(GetTestDataTable(name, true, true, SelectRowCount));
            stopwatch.Reset();
            stopwatch.Start();

            for (int i = 0; i < SelectCount; i++)
            {
                ds.Tables[0].Select(string.Format("{0} = {1} and {2} = {3}", "a", i, "b", i));
            }

            stopwatch.Stop();
            ds.Dispose();
            trd.Duration = stopwatch.Elapsed.TotalSeconds;
            return trd;
        }

        private static DataTable GetTestDataTable(string tableName, bool indexLong, bool indexFirstThree, int numRows)
        {
            DataTable dt = new DataTable(tableName);
            dt.Columns.Add("a", typeof(long));
            dt.Columns.Add("b", typeof(string));
            dt.Columns.Add("c", typeof(string));
            dt.Columns.Add("d", typeof(string));
            dt.Columns.Add("e", typeof(string));
            dt.Columns.Add("f", typeof(string));
            dt.Columns.Add("g", typeof(string));

            if (indexLong && indexFirstThree)
            {
                DataColumn[] key = new DataColumn[dt.Columns.Count];
                for (int c = 0; c < key.Length; c++)
                {
                    key[c] = dt.Columns[c];
                }
                dt.PrimaryKey = key;
            }
            else if (indexLong)
            {
                dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
            }
            else if (indexFirstThree)
            {
                dt.PrimaryKey = new DataColumn[] { dt.Columns[0], dt.Columns[1], dt.Columns[2] };
            }

            DataRow dr;
            for (int i = 0; i < numRows; i++)
            {
                dr = dt.NewRow();
                dr[0] = i;
                for (int j = 1; j < dt.Columns.Count; j++)
                {
                    dr[j] = i.ToString();
                }
                dt.Rows.Add(dr);
            }

            dt.AcceptChanges();
            return dt;
        }

    }
}
